﻿/*********************弘石项目三组************************
*  功能描述：联系人信息控制器
*  作者：刘伟康
*  时间：2021-12-12
*  修改人：刘伟康
*  修改时间：2021-12-12
*********************弘石项目三组************************/
using Hong.Business.Business_CustomerManagement;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MiniExcelLibs;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace WeHongshibApI.Controllers.Controllers_CustomerManagement
{
    [ApiExplorerSettings(GroupName = "Customer")]
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class CustInfoPersonListController : ControllerBase
    {
        #region 构造器注入
        private readonly CustInfoPersonListBusiness _custInfo;
        public CustInfoPersonListController(CustInfoPersonListBusiness custInfo)
        {
            _custInfo = custInfo;
        }
        #endregion
        #region 联系人信息
        #region 联系人查询分页
        /// <summary>
        /// 联系人查询分页
        /// </summary>
        /// <param name="personNmae"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetCustomerExPersonView(string personNmae, int pageIndex = 1, int pageSize = 10)
        {
            int totalCount;
            var list = _custInfo.GetCustomerExPersonView(personNmae, pageIndex, pageSize, out totalCount);
            return Ok(new { list, totalCount });
        }
        #endregion

        #region 导出excel
        [HttpGet]
        public IActionResult DownloadExcel()
        {
            int totalCount;
            var list = _custInfo.GetCustomerExPersonView(null, 1, 1000000, out totalCount);
            var values = new List<Dictionary<string, string>>();
            foreach (var item in list)
            {
                values.Add(new Dictionary<string, string>
                {
                    {"客户编号",$"{item.CustId}" }, {"客户名称",$"{item.CustName}" }, {"邮政编码",$"{item.CustEmail}" },
                    {"公司电话",$"{item.CustPhone}" }, {"客户简称",$"{item.CustAbbreviation}" }, {"公司地址",$"{item.CustAddress}" },
                    {"联系人",$"{item.CustPhones}" }, {"传真号",$"{item.CustFaxNumber}" }, {"扩展主键",$"{item.EdId}" },
                    {"客户英文名",$"{item.EdName}" }, {"是否合作",$"{item.EdIsheZuo}" }, {"所在国家",$"{item.EdCountryId1}" },
                    {"所在省份",$"{item.EdCountryId2}" }, {"所在城市",$"{item.EdCountryId3}" }, {"客户类型",$"{item.EdTypeId}" },
                    {"所属行业",$"{item.EdIndustryId}" }, {"邮箱",$"{item.EdEmail}" }, {"关系建立时间",$"{item.EdCreateTime}" },
                    {"关系部门",$"{item.EdBuMen}" }, {"开启银行名称",$"{item.EdBankName}" }, {"企业代码",$"{item.EdEnterprise}" },
                    {"开启银行账号",$"{item.EdBankAccount}" }, {"备注",$"{item.EdBeiZhu}" }, {"客户基本信息（外键）",$"{item.CustomerId}" },
                    {"字典主键",$"{item.DicId}" }, {"字典名称",$"{item.DicName}" }, {"字典主键1",$"{item.DicId1}" },
                    {"字典名称1",$"{item.DicName1}" },{"序号",$"{item.PersonId}" },{"姓名",$"{item.PersonNmae}" },
                    {"职务",$"{item.PersonZhiWu}" },{"电话",$"{item.PersonPhone}" },{"部门",$"{item.PersonBuMen}" },
                    {"住宅电话",$"{item.PersonShangWuD}" },{"商务传真",$"{item.PersonShangWuC}" },{"住宅传真",$"{item.PersonZhuZhaiC}" },
                    {"email",$"{item.PersonEmail}" },{"负责人备注",$"{item.PersonBeiZhu}" }
                });
            }   
            var memoryStream = new MemoryStream();
            memoryStream.SaveAs(values);
            memoryStream.Seek(0, SeekOrigin.Begin);
            return new FileStreamResult(memoryStream, "applicatio/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
            {
                FileDownloadName = "联系人信息.xlsx"
            };
        }
        #endregion
        #endregion

    }
}
